package com.administrator.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.entity.User;
import com.util.BaseDao;


public class UserDao {//用户的查询，增加，删除
	
	/*
	 * 查询所有用户方法
	 */
	public List<User> findAllUser(){
		try{
			  List<User> list=new ArrayList<User>(); 
			  PreparedStatement ps=null;
			  Connection conn=BaseDao.getConnection();
			  ps=conn.prepareStatement("select * from user_shop");
			  ResultSet rs=ps.executeQuery();
			  User p=null;
			  while(rs.next()){
				  p=new User();
				  		p.setId(rs.getInt(1));
				  		p.setUsername(rs.getString(2));
				  		p.setPassword(rs.getString(3));
				  		list.add(p);
				  		System.out.println(p);
			  		}
			  		return list; 
			} catch (SQLException e) {
					e.printStackTrace();
					return null;
			}		
	}
	
	/*
	 * 添加用户方法
	 */
	public boolean addUser(User u){
		try{
			Connection con=BaseDao.getConnection();
			PreparedStatement pstm=con.prepareStatement("insert into user_shop(id,username,password) values(?,?,?)");
			pstm.setInt(1, u.getId());
			pstm.setString(2, u.getUsername());
			pstm.setString(3, u.getPassword());
			int rs = pstm.executeUpdate();
			if(rs>0){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	

	public boolean isExist(int id) {
		// TODO Auto-generated method stub
		return false;
	}
	public boolean isExist(String string) {
		// TODO Auto-generated method stub
		return false;
	}

}
